﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using LeEdu.MODEL;
using LeEdu.BLL;
using System.IO;
using LeEdu.DAL;



namespace LeEdu.Web.Controllers
{
    public class HomeController : Controller
    {

        
        #region  实例化BLL层
        /// <summary>   
        /// 实例化UserInfoBLL
        /// </summary>
        UserInfoBLL userbll = new UserInfoBLL();

        /// <summary>
        /// 实例化UserInfo
        /// </summary>
        BaseBLL<UserInfo> bll = new BaseBLL<UserInfo>();

        /// <summary>
        /// 实例化笔记BLL
        /// </summary>
        NoteBLL notebll = new NoteBLL();

        /// <summary>
        /// 实例化CourseBLL
        /// </summary>
        CourseBLL coursebll = new CourseBLL();

        /// <summary>
        /// 实例化CollegeBLL
        /// </summary>
        CollegeBLL collegebll = new CollegeBLL();

        /// <summary>
        /// 实例化LableBLL
        /// </summary>
        LableBLL lablebll = new LableBLL();

        /// <summary>
        /// 实例化HourBLL
        /// </summary>
        HourBLL hourbll = new HourBLL();

        /// <summary>
        /// 实例化StuBLL
        /// </summary>
        StuBLL stubll = new StuBLL();

        /// <summary>
        /// 实例化EstimateBLL
        /// </summary>
        EstimateBLL estibll = new EstimateBLL();

        /// <summary>
        /// 实例化UserInfoBLL
        /// </summary>
        UserInfoBLL userinfobll = new UserInfoBLL();

        /// <summary>
        /// 实例化ClassTableBLL
        /// </summary>
        ClassTableBLL classtablebll = new ClassTableBLL();

        /// <summary>
        /// 实例化TopicBLL
        /// </summary>
        TopicBLL topicbll = new TopicBLL();

        /// <summary>
        /// 实例化course_classbll
        /// </summary>
        Course_ClassBLL course_classbll = new Course_ClassBLL();

        /// <summary>
        /// 实例化notebll
        /// </summary>
        NoteBLL notesbll = new NoteBLL();

        /// <summary>
        /// 实例化PurchaseBLL
        /// </summary>
        PurchaseBLL purchasebll = new PurchaseBLL();
        #endregion

        #region 显示主页面,并初始化数据
        /// <summary>
        /// <summary>
        /// 显示主页面,并初始化数据
        /// </summary>
        /// <returns></returns>fd
     
        public ActionResult Index()
        {
            UserInfo users = new UserInfo();
            
            College colleges = new College();
            Lable lables = new Lable();
            int count = userbll.getEntityList(p => true).Count();

            //switch (id)
            //{
            //    case 1: int counts = bll.chaxun(c => c.CoursesName == ".NET").Count(); break;


            //    default:break;
            //}

            #region
            //判断数据库用户表是否有数据,有数据正常显示数据
            if (count > 0)
            {
               
                List<Hour> list2 = hourbll.getEntityList(p => true).Take(5).OrderByDescending(p => p.course.CreateTime).Where(p => p.course.Cpublish == 1).ToList();
                ViewBag.list2 = list2;
                List<UserInfo> lst3 = userbll.getEntityList(p => true).Where(P => (P.RoleId == 2) && (P.Tisornot == 1)).Take(4).ToList();
                List<Courses> list = coursebll.getEntityList(p => true).Take(9).OrderByDescending(p => p.CreateTime).Where(p => p.Cpublish == 1 && p.TJisornot == 1).ToList();
                ViewBag.list = list;
                //读取班级信息
                List<ClassTable> classlist = classtablebll.getEntityList(p => true).Take(4).ToList();
                int count2 = classtablebll.getEntityList(p => true).Count();
                ViewBag.count2 = count2;
                ViewBag.classlist = classlist;
                //计算推荐教师条数
                int cout1 = userbll.getEntityList(p => true).Where(p => p.Tisornot == 1).Count();
                ViewBag.cout1 = cout1;
                ViewBag.lst3 = lst3;
                ViewBag.GroupCount = 0;
                ViewBag.HuaCount = 0;
                if (new BaseBLL<Group>().GetAll() != null)
                {
                    ViewBag.GroupCount = new BaseBLL<Group>().GetAll().Count();
                }
                if (new BaseBLL<Topicshi>().GetAll() != null)
                {
                    ViewBag.HuaCount = new BaseBLL<Topicshi>().GetAll().Count();
                }


                ViewBag.Group = new BaseBLL<Group>().GetAll().Take(6).ToList();
                ViewBag.TOPIC = new BaseBLL<Topicshi>().GetAll().Take(4).ToList();
                CourseBLL bll1 = new CourseBLL();
            List<Courses> lst= bll1.chaxun(c=>true).ToList();
                //

            DbContextEntity entity = new DbContextEntity();
            List<Lable> lablelist = entity.Lables.ToList();
                //
            ViewBag.lable = lablelist;
                //
            List<Courses> courselist = entity.Courses.ToList();
            ViewBag.list1 = courselist; 
                return View(lst);
            }
            else
            {
                //数据库用户表无数据,初始化向用户表,课程表,标签表插入一条初始化数据
                users.Email = "admin@qq.com";
                users.RoleId = 0;
                users.Uname = "admin";
                users.Upwd = "admin";
                users.Tisornot = 0;
                users.TeacherCheck = 1;
                users.UTime = DateTime.Now;
                colleges.CollegeName = "初级";
                lables.LableName = "mvc";

                if (userinfobll.AddEntity(users))
                {
                    if (collegebll.AddEntity(colleges))
                    {
                        if (lablebll.AddEntity(lables))
                        {
                            List<Courses> list = coursebll.getEntityList(p => true).Take(9).OrderByDescending(p => p.CreateTime).Where(p => p.Cpublish == 1 && p.TJisornot == 1).ToList();
                            ViewBag.list = list;
                            List<Hour> list2 = hourbll.getEntityList(p => true).Take(5).OrderByDescending(p => p.course.CreateTime).Where(p => p.course.Cpublish == 1).ToList();
                            ViewBag.list2 = list2;

                            return View();
                        }
                        return View();
                    }
                    return View();
                }
                else
                {
                    return View();
                }

            }
            #endregion
        }
        #endregion
        //课程界面显示
        public ActionResult Kecheng( int id)
        {
            DbContextEntity entity = new DbContextEntity();
            List<Courses> list;
            if (id == 0)
            {
                list = entity.Courses.ToList();
            }
            else
            {
                list= entity.Courses.Where(p => p.LableId == id).ToList();
            }
            
            return Json(list,JsonRequestBehavior.AllowGet);
        }


       //根据类型进行显示
        public ActionResult lei(string name)
        {
            DbContextEntity entity = new DbContextEntity();
            List<Courses> list;
         
            list = entity.Courses.Where(p => p.CSutTitle == name).ToList();
           

            return Json(list, JsonRequestBehavior.AllowGet);
        }






        #region 更多教师
        /// <summary>
        /// 更多教师
        /// </summary>
        /// <returns></returns>
        public ActionResult TuijianTeacher()
        {       
            List<UserInfo> lst = userbll.getEntityList(t => true).Where(t => t.RoleId == 2).ToList();
            ViewBag.Teathar = lst;
            return View(lst);
        }
        #endregion

        #region 教师详情显示
        /// <summary>
        /// 教师详情显示
        /// </summary>
        /// <returns></returns>
        public ActionResult TeacherShow(int id=1)
        {
            if (Session["id"] == null)
            {
                return Content("<script>alert('请先登录');location.href='/Login/Index'</script>");
            }
            UserInfo lst1 = userbll.Search(id);
            ViewBag.lst1 = lst1;
            List<Courses> lst2 = coursebll.getEntityList(p => true).Where(p => p.UserInfoId == lst1.UserInfoId).ToList();
            return View(lst2);
        }
        #endregion

        #region 个人设置页面
        /// <summary>
        /// 个人设置页面
        /// </summary>
        /// <param name="id">用户ID</param>
        /// <returns></returns>
        public ActionResult Personage(int id)
        {
            //判断session是否过期
            if (Session["id"] == null)
            {
                return Content("<script>alert('请先登录');location.href='/Login/Index'</script>");
            }
            else
            {
                int pid = id;
                Session["pid"] = pid;
                UserInfo user = userbll.Search(id);
                return View(user);
            }
        }
        #endregion

        #region 注销登陆
        /// <summary>
        /// 注销登陆
        /// </summary>
        /// <returns></returns>
        public ActionResult Exit()
        {
            Session["User"] = null;
            return Content("<script>location.href='/Login/Index'</script>");
        }
        #endregion

        #region 修改用户密码
        /// <summary>
        /// 修改用户密码
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Personage()
        {
            int id = Convert.ToInt32(Request["id"]);
            string pwd = Request["pwd"].ToString();
            UserInfo user = userbll.Search(id);
            user.Upwd = pwd;
            if (userbll.Edit(user))
            {
                return Content("1");
            }
            else
            {
                return Content("0");
            }
        }
        #endregion

        #region 修改用户名
        /// <summary>
        /// 修改用户名
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult EditName()
        {
            int id = Convert.ToInt32(Request["id"]);
            string Uname = Request["Uname"].ToString();
            UserInfo user = userbll.Search(id);
            user.Uname = Uname;
            Session["Uname"] = Uname;
            if (userbll.Edit(user))
            {
                return Content("1");
            }
            else
            {
                return Content("0");
            }
        }
        #endregion

        #region 修改用户
        /// <summary>
        /// 修改用户
        /// </summary>
        /// <returns></returns>
        public ActionResult EditUser()
        {
            //判断session是否过期
            if (Session["id"] == null)
            {
                return Content("<script>alert('请先登录');location.href='/Login/Index'</script>");
            }
            else
            {
                int id = Convert.ToInt32(Request["id"]);
                string PhoneNum = Request["PhoneNum"].ToString();
                string XingMing = Request["XingMing"].ToString();
                int Sex = Convert.ToInt32(Request["Sex"]);
                string Job = Request["Job"].ToString();
                string Post = Request["Post"].ToString();
                string QQ = Request["QQ"].ToString();
                string WeChat = Request["WeChat"].ToString();
                string Self = Request["Self"].ToString();
                UserInfo user = userbll.Search(id);
                user.PhoneNum = PhoneNum;
                user.XingMing = XingMing;
                user.Sex = Sex;
                user.Job = Job;
                user.Post = Post;
                user.QQ = QQ;
                user.WeChat = WeChat;
                user.Self = Self;
                if (userbll.Edit(user))
                {
                    return Content("1");
                }
                else
                {
                    return Content("0");
                }
            }
        }
        #endregion

        #region 课时管理页面
        /// <summary>
        /// 课时管理页面
        /// </summary>
        /// <param name="id">课程id</param>
        /// <returns></returns>
        public ActionResult ClassHour(int id)
        {
            Courses course = coursebll.Search(id);
            List<Hour> list = hourbll.getEntityList(p => true).Where(p => p.CoursesID == id).ToList();
            ViewBag.course = course;
            ViewBag.list = list;
            Session["ClassId"] = id;
            List<Student> listu = stubll.getEntityList(p => true).Take(9).ToList();
            ViewBag.stu = listu;
            return View();
        }
        #endregion

        #region 更换学员
        /// <summary>
        /// 更换学员
        /// </summary>
        /// <returns></returns>
        public ActionResult huan()
        {
            StuBLL stubll = new StuBLL();
            List<Student> listu = stubll.getEntityList(p => true).OrderByDescending(p => p.StudentId).Take(9).ToList();
            List<Student> lst = new List<Student>();
            return Json(listu, JsonRequestBehavior.AllowGet);
        }
        #endregion

        #region 视频播放
        /// <summary>
        /// 视频播放
        /// </summary>
        /// <param name="id">视频id</param>
        /// <returns></returns>
        public ActionResult VideoPlay(int id)
        {
            int vid = Convert.ToInt32(Session["ClassId"]);
            Hour hours = hourbll.Search(id);
            List<Hour> list = hourbll.getEntityList(p => true).Where(p => p.CoursesID == vid).ToList();
            //之前写的List<Courses> list1 = coursebll.getEntityList(p => true).Where(p => p.CoursesId == vid).ToList();
            Courses list1 = coursebll.Search(vid);
            ViewBag.note = notebll.getEntity(t => t.HourId == id && t.CoursesID == list1.CoursesId);
            ViewBag.hours1 = list;
            ViewBag.list1 = list1;
            ViewBag.hourid = id;
            ViewBag.hours = hours;
            return View();
        }
        #endregion

        #region 课程管理页面
        /// <summary>
        /// 课程管理页面
        /// </summary>
        /// <returns></returns>
        public ActionResult Boutique()
        {
            ViewBag.list = coursebll.getEntityList(p => true).Where(p => p.Cpublish == 1).ToList();
            return View();
        }
        #endregion

        #region 地址栏输入错误地址处理
        /// <summary>
        /// 地址栏输入错误地址处理
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        public ActionResult Error(string code)
        {
            return Content("<script>location.href='/Home/Index'</script>");
        }
        #endregion

        #region 个人头像修改
        /// <summary>
        /// 个人头像更改
        /// </summary>
        /// <param name="Avatar">头像图片</param>
        /// <returns></returns>
        public ActionResult FileImg(UserInfo user, HttpPostedFileBase Avatar)
        {
            int id = Convert.ToInt32(Session["pid"]);
            //if (Avatar.ContentLength > 0)
            //{
                string FileNmae = DateTime.Now.ToString("yyyyMMddhhmmss") + Path.GetExtension(Avatar.FileName);
                string filePath = Path.Combine(Server.MapPath("~/Img"), FileNmae);
                Avatar.SaveAs(filePath);
                user.Avatar = FileNmae;
                if (userbll.Edit(user))
                {
                    Session["User"] = user;
                    return Content("<script>alert('头像修改成功');location.href='/Home/Personage?id=" + id + "'</script>");
                }
                else
                {
                    return Content("<script>alert('头像修改失败');location.href='/Home/Personage?id=" + id + "'</script>");
                }
            //}
            //else
            //{
            //    return View("Personage");
            //}
        }
        #endregion


        #region 修改订单信息
        /// <summary>
        /// 修改订单信息
        /// </summary>
        /// <returns></returns>
        public ActionResult jilu()
        {
            //商户订单号
            string sta = Request.QueryString["out_trade_no"];

            Purchase stb = purchasebll.getEntity(t => t.OrderNumber == sta);
            //获取交易号
            //stb.BTRADENUMBER = Request.QueryString["trade_no"].ToString();
            //if (Request.Form["trade_status"] == "WAIT_BUYER_PAY")
            //{//该判断表示买家已在支付宝交易管理中产生了交易记录，但没有付款
            //    //stb.BTRADESTATE = 0;
            //}
            //else if (Request.Form["trade_status"] == "WAIT_SELLER_SEND_GOODS")
            //{//该判断示买家已在支付宝交易管理中产生了交易记录且付款成功，但卖家没有发货

            //    stb.BTRADESTATE = 1;
            //}
            //else if (Request.Form["trade_status"] == "WAIT_BUYER_CONFIRM_GOODS")
            //{//该判断表示卖家已经发了货，但买家还没有做确认收货的操作
            //    stb.BTRADESTATE = 2;
            //}
            //else if (Request.Form["trade_status"] == "TRADE_FINISHED")
            //{//该判断表示买家已经确认收货，这笔交易完成
            //    stb.BTRADESTATE = 3;
            //}
            purchasebll.Edit(stb);
            return RedirectToAction("ClassHour");
        }
        #endregion


        #region 在线问答
        /// <summary>
        /// 在线问答
        /// </summary>
        /// <returns></returns>
        public ActionResult question()
        {
            int id = Convert.ToInt32(Session["ClassId"]);
            Courses course = coursebll.Search(id);
            List<Hour> list = hourbll.getEntityList(p => true).Where(p => true).ToList();
            ViewBag.course = course;
            ViewBag.list = list;
            //全部评论
            //全部的course id
            List<Courses> listcourse = coursebll.getEntityList(p => true).ToList();

            CommentBLL bll = new CommentBLL();
            List<Comment> lst = bll.getEntityList(p => p.HourId == id);
            //算出全部评论
            var query = from m in hourbll.getEntityList(p => true)
                        join n in bll.getEntityList(p => true) on m.HourId equals n.HourId
                        join f in listcourse on m.CoursesID equals f.CoursesId
                        select n;
            //计算评论条数
            List<Comment> listAll = bll.getEntityList(p => true);
            List<Comment> listcomment = query.Where(p => p.Cid == 0).OrderByDescending(p => p.PostTime).ToList();
            foreach (var item in listcomment)
            {
                item.Talker = (from m in listAll
                               where m.Cid == item.CommentId
                               select m).ToList().Count();
            }
            ViewBag.coment = listcomment;
            return View();
        }
        #endregion

        #region 评论
        /// <summary>
        /// 评论
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public string question2()
        {
            CommentBLL bll = new CommentBLL();
            List<Courses> listcourse = coursebll.getEntityList(p => true).ToList();
            List<Comment> lst = new List<Comment>();
            //算出全部评论
            var query = from m in hourbll.getEntityList(p => true)
                        join n in bll.getEntityList(p => true) on m.HourId equals n.HourId
                        join f in listcourse on m.CoursesID equals f.CoursesId
                        select n;

            //下拉框传过来的课时id
            int id = Convert.ToInt32(Request["id"]);
            //已读
            if (Request["type"] == "1")
            {
                // lst = bll.getEntityList(p => true).Where(p => (p.CType == 1) && id == 0 ? true : p.HourId == id).ToList();
                query = query.Where(p => (p.CType == 1) && (id == 0 ? true : p.HourId == id) && p.Cid == 0).ToList();
            }
            //未读
            if (Request["type"] == "2")
            {
                //lst = bll.getEntityList(p => true).Where(p => (p.CType == 0) && id == 0 ? true : p.HourId == id).ToList();
                query = query.Where(p => (p.CType == 0) && (id == 0 ? true : p.HourId == id) && p.Cid == 0).ToList();
            }
            //全部
            if (Request["type"] == "3")
            {
                //lst = bll.getEntityList(p => true).Where(p => id == 0 ? true : p.HourId == id).ToList();
                query = query.Where(p => (id == 0 ? true : p.HourId == id) && p.Cid == 0).ToList();
            }
            string html = "";
            html += " <div class=\"question-item\">";

            foreach (var item in query.ToList())
            {
                //判断问题的类型
                var type = "";
                if (item.CType == 0)
                {
                    type = "未回答";
                }
                if (item.CType == 1)
                {
                    type = "已回答";
                }

                html += "<div class=\"question-notanswer\">" + type + "</div>";
                html += " <div class=\"question-content\">";
                html += " <a href='/Home/VideoPlay/" + item.CommentId + "' target=\"_blank\"><pre style=\"margin:0px\" class=\"question-text notice15\" questionid=\"4480\">" + item.Content + "</pre></a>";
                html += "  <div class=\"question-time detial13\">";

                html += "  <div style=\"float: left;margin-right:10px;\"><span style=\"font-weight: bold\">" + item.CPeople + "</span><span>提问于</span><span>" + item.PostTime + "</span> <span>来自于</span></div>";

                html += " <a class=\"section-link\" href='#'><div title=\"13.深度探索list上\" style=\"max-width: 230px; text-overflow: ellipsis; overflow: hidden;float:left;margin-right:5px; white-space: nowrap; display: block\">" + item.Hour.HTitle + "</div></a> ";

                html += "  <div style=\"float: left\"> <a href='/Home/questiondetail/" + item.CommentId + "' target=\"_blank\" style=\"color: #3890CA\" questionid=\"4480\">  <span>讨论( " + item.Talker + " )</span>  </a>   </div>";

                html += "   </div></div>";

                html += " <div class=\"clearBoth\"></div>";
            }
            html += "</div>";
            return html;
        }
        #endregion


        #region 评论详细
        /// <summary>
        /// 评论详细
        /// </summary>
        /// <param name="id">评论ID</param>
        /// <returns></returns>

        public ActionResult questiondetail(int id)
        {
            Session["Cid"] = id;
            UserInfo u = new UserInfo();
            int id2 = Convert.ToInt32(Session["ClassId"]);
            Courses course = coursebll.Search(id2);
            List<Hour> list = hourbll.getEntityList(p => true).Where(p => p.CoursesID == id).ToList();
            ViewBag.course = course;
            ViewBag.list = list;
            //全部评论
            CommentBLL bll = new CommentBLL();
            Comment ct = bll.Search(id);
            Hour hr = hourbll.getEntity(p => p.CoursesID == course.CoursesId);
            ViewBag.hour = hr;
            ViewBag.coment = ct;
            return View();
        }
        #endregion

        #region 调用方法,显示楼层
        /// <summary>
        /// 调用方法,显示楼层
        /// </summary>
        /// <returns></returns>
        public string showdetail()
        {
            int id = Convert.ToInt32(Session["Cid"]);

            CommentBLL bll = new CommentBLL();
            List<Comment> lst = bll.getEntityList(p => true).Where(p => p.Cid == id).ToList();

            string html = "";
            foreach (var item in lst)
            {
                html += " <div class=\"question-detail-account\"> <img style=\"width:100%\" src=\"/UploadResources/HeadImage/3ac49616-b775-4290-935b-e41cb1885967.jpg\">   </div>";

                html += " <div class=\"question-detail-text\"> <pre class=\"question-detail-content notice15\" style=\"margin: 0px;\">" + item.Content + "</pre>";

                html += "  <div class=\"question-detail-name detial13\">  <div style=\"float: left\">  <span style=\"font-weight: bold\">" + item.CPeople + "</span><span>讨论于</span>     <span>" + item.PostTime + "</span> </div>";

                html += " <div style=\"float: right;margin-right:10px\"> " + item.CFoolr + "楼</div><div class=\"clearBoth\"></div> </div>     </div>";
            }
            return html;
        }
        #endregion

        #region 盖楼
        /// <summary>
        /// 盖楼
        /// </summary>
        /// <param name="id">上一级评论ID</param>
        /// <param name="ct">评论对象</param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult questiondetail(int id, Comment ct)
        {
            CommentBLL bll = new CommentBLL();
            Comment cc = bll.Search(id);
            cc.Content = ct.Content;
            cc.Cid = id;
            cc.Talker += 1;
            cc.CFoolr += 1;
            bll.Edit(cc);
            return Content("<script>;location.href='/Home/questiondetail/" + id + "';</script>");
        }
        #endregion

        #region 具体盖楼方法
        /// <summary>
        /// 具体盖楼方法
        /// </summary>
        /// <param name="id">上一级评论ID</param>
        /// <param name="ct">评论对象</param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult questiondetailadd(int id, Comment ct)
        {
            CommentBLL bll = new CommentBLL();
            Comment cc = bll.Search(id);
            Comment last = bll.getEntityList(p => true).Where(p => p.Cid == id).OrderByDescending(p => p.PostTime).Take(1).FirstOrDefault();
            cc.Cid = id;
            cc.Content = ct.Content;
            cc.Talker += 1;
            cc.CFoolr = last == null ? 1 : last.CFoolr + 1;
            cc.HourId = ct.HourId;
            cc.PostTime = DateTime.Now;
            cc.CType = ct.CType;
            cc.CPeople = ct.CPeople;
            bll.AddEntity(cc);
            return Content("<script>;location.href='/Home/questiondetail/" + id + "';</script>");
        }
        #endregion

        #region 资源下载
        /// <summary>
        /// 资源下载
        /// </summary>
        /// <returns></returns>
        public ActionResult download()
        {
            int id = Convert.ToInt32(Session["ClassId"]);
            Courses course = coursebll.Search(id);
            List<Hour> list = hourbll.getEntityList(p => true).Where(p => p.CoursesID == id && p.HType != 0).ToList();
            ViewBag.course = course;
            ViewBag.list = list;
            List<Files> fl = new FilesBLL().getEntityList(p => true).ToList();
            ViewBag.fl = fl;
            List<Student> listu = stubll.getEntityList(p => true).Take(9).ToList();
            ViewBag.stu = listu;
            return View();
        }
        #endregion
        public ActionResult Myexam()
        {
            int id = Convert.ToInt32(Session["Courseid"]);
            LeEdu.BLL.ExamBLL bll = new LeEdu.BLL.ExamBLL();
            List<LeEdu.MODEL.Exam> list = bll.getEntityList(p => p.CoursesId == id && p.PubState == 1);
            return View(list);
        }
                                   
        #region 我要提问
        /// <summary>
        /// 我要提问
        /// </summary>
        /// <returns></returns>
        public ActionResult newquestion()
        {
            int id = Convert.ToInt32(Session["ClassId"]);
            Courses course = coursebll.Search(id);
            //List<Hour> list = hourbll.getEntityList(p => true).Where(p => p.CoursesID == id).ToList();
            List<Hour> list = hourbll.getEntityList(p => true).Where(p => true).ToList();
            ViewBag.course = course;
            ViewBag.list = list;
            return View();
        }
        #endregion

        #region 添加评论
        /// <summary>
        /// 添加评论
        /// </summary>
        /// <param name="c">评论对象</param>
        /// <returns></returns>
        public ActionResult Addquestion(Comment c)
        {
            c.Cid = 0;
            UserInfo u = new UserInfo();
            u.Uname = Session["Uname"].ToString();
            CommentBLL bll = new CommentBLL();
            if (bll.AddEntity(c))
            {
                return RedirectToAction("question");
            }
            else
            {
                return RedirectToAction("newquestion");
            }
        }
        #endregion

        #region 根据班级ID显示对应班级详细信息
        /// <summary>
        /// 根据班级id显示对应班级详细信息
        /// </summary>
        /// <param name="id">班级ID</param>
        /// <returns></returns>
        public ActionResult Showcpp(int id)
        {
            //根据班级id找对应的课程id
            ClassTable cb = classtablebll.Search(id);
            //实例化一个笔记集合
            List<Note> nlist = new List<Note>();
            //显示对应的班级集合
            List<Course_Class> ctlist = course_classbll.getEntityList(p => true).Where(p => p.ClassTableId == id).ToList();
            //循环班级集合显示对应的笔记内容
            foreach (var item in ctlist)
            {
                nlist.AddRange(notesbll.getEntityList(p => true).Where(p => p.CoursesID == item.CoursesId).ToList().Take(5));
            }
            ViewBag.cb = cb;
            ViewBag.ctlist = ctlist;
            //对应课程笔记集合
            ViewBag.nlist = nlist;
            return View();
        }
        #endregion

        #region 点赞笔记
        /// <summary>
        /// 点赞笔记
        /// </summary>
        /// <returns></returns>
        public ActionResult DzNotes(int id)
        {
            Note kk = notebll.getEntityList(p => true).Where(p => p.ID == id).FirstOrDefault();
            kk.Like = kk.Like + 1;
            if (notebll.Edit(kk))
            {
                return RedirectToAction("ShowNotes");
            }
            else
            {
                return Content("0");
            }
        }
        #endregion

        #region 查看笔记
        /// <summary>
        /// 查看笔记
        /// </summary>
        /// <returns></returns>
        public ActionResult ShowNotes()
        {
            int id = Convert.ToInt32(Session["ClassId"]);
            Courses course = coursebll.Search(id);

            List<Hour> list = hourbll.getEntityList(p => true).Where(p => true).ToList();
            ViewBag.course = course;
            ViewBag.list = list;

            List<Note> k = notebll.getEntityList(p => true).Take(5).OrderByDescending(p => p.NoteTime).Where(p => p.Share == 1 && p.CoursesID == course.CoursesId).ToList();
            ViewBag.k = k;

            List<Note> fk = notebll.getEntityList(p => true).OrderByDescending(p => p.NoteTime).Where(p => p.Share == 1 && p.CoursesID == course.CoursesId).ToList();
            ViewBag.fk = fk;

            List<Student> listu = stubll.getEntityList(p => true).Take(9).ToList();
            ViewBag.stu = listu;
            return View();
        }
        #endregion

        #region 添加笔记
        /// <summary>
        /// 添加笔记
        /// </summary>
        /// <param name="n">笔记对象</param>
        /// <returns></returns>
        public ActionResult SaveNote(Note n)
        {
            n.NoteTime = DateTime.Now;
            if (notebll.AddEntity(n))
            {
                ViewBag.n = n.NoteContent;
                return Content("1");
            }
            else
            {
                return Content("0");
            }
        }
        #endregion

        #region 修改笔记
        /// <summary>
        /// 修改笔记
        /// </summary>
        /// <param name="n">笔记对象</param>
        /// <returns></returns>
        public ActionResult EditNote(Note n)
        {
            n.NoteTime = DateTime.Now;

            if (notebll.Edit(n))
            {
                ViewBag.n = n.NoteContent;
                return Content("1");
            }
            else
            {
                return Content("0");
            }
        }
        #endregion

        #region 评论
        /// <summary>
        /// 评论
        /// </summary>
        /// <returns></returns>
        public ActionResult Estimat()
        {
            if (Session["id"] == null)
            {
                return RedirectToAction("Index", "Login");
            }
            else
            {
                int id = Convert.ToInt32(Session["ClassId"]);
                Courses course = coursebll.Search(id);

                List<Hour> list = hourbll.getEntityList(p => true).Where(p => true).ToList();
                ViewBag.course = course;
                ViewBag.list = list;

                List<Student> listu = stubll.getEntityList(p => true).Take(9).ToList();
                ViewBag.stu = listu;

                ViewBag.esti = estibll.getEntityList(t => true).ToList();

                return View();
            }
        }
        #endregion

        #region 添加评论
        /// <summary>
        /// 添加评论
        /// </summary>
        /// <param name="e">评价对象</param>
        /// <returns></returns>
        public ActionResult Estimats(Estimate e)
        {
            e.EstimateTime = DateTime.Now;
            if (estibll.AddEntity(e))
            {
                return Content("1");
            }
            else
            {
                return Content("0");
            }
        }
        #endregion

        #region 重新评论
        /// <summary>
        /// 重新评论
        /// </summary>
        /// <param name="e">评价对象</param>
        /// <returns></returns>
        public ActionResult Estimatcx(Estimate e)
        {
            e.EstimateTime = DateTime.Now;
            if (estibll.Edit(e))
            {
                return Content("1");
            }
            else
            {
                return Content("0");
            }
        }
        #endregion

        #region 我的定单页面
        /// <summary>
        /// 我的订单页面
        /// </summary>
        /// <returns></returns>
        public ActionResult MyOrder()
        {
            return View();
        }
        #endregion

        #region 话题前台显示
        /// <summary>
        /// 话题前台显示
        /// </summary>
        /// <param name="id">话题ID</param>
        /// <returns></returns>
        public ActionResult topics(int? id)
        {
            //接到课程ID
            int CourseID = Convert.ToInt32(Session["ClassId"]);
            Courses course = coursebll.Search(CourseID);
            ViewBag.course = course;
            //查询出所有的帖子
            List<Topic> list = topicbll.getEntityList(t => true).Where(t => (t.TId == 0) && ((id == null) ? true : t.Attribute == 1)).ToList();
            List<Student> listu = stubll.getEntityList(p => true).Take(9).ToList();
            ViewBag.stu = listu;
            return View(list);
        }
        #endregion

        #region 话题浏览量统计
        /// <summary>
        /// 话题浏览量统计
        /// </summary>
        /// <param name="id">话题ID</param>
        /// <returns></returns>
        public ActionResult showTop(int id)
        {
            Topic topic = topicbll.Search(id);
            topic.See = topic.See + 1;
            bool s = topicbll.Edit(topic);
            ViewBag.topic = topic;
            int idd = Convert.ToInt32(Session["ClassId"]);
            Courses course = coursebll.Search(idd);
            ViewBag.course = course;
            return View();
        }
        #endregion

        #region 调用方法显示楼层
        /// <summary>
        /// 调用方法显示楼层
        /// </summary>
        /// <returns></returns>
        public string showtopic2()
        {
            int topicid = Convert.ToInt32(Request["topicid"]);
            List<Topic> lst = topicbll.getEntityList(p => true).Where(p => p.TId == topicid).ToList();
            string html = "";
            foreach (var item in lst)
            {
                html += " <div class=\"question-detail-account\"> <img style=\"width:100%;height:100px\" src=\"/Img/" + item.UserInfo.Avatar + "\">   </div>";

                html += " <div class=\"question-detail-text\"> <pre class=\"question-detail-content notice15\" style=\"margin: 0px;\">" + item.TopicContent + "</pre>";

                html += "  <div class=\"question-detail-name detial13\">  <div style=\"float: left\">  <span style=\"font-weight: bold\">" + item.UserInfo.Uname + "</span><span>讨论于</span>     <span>" + notebll.DateStringFromNow(item.TopicTime) + "</span> </div>";

                html += " <div style=\"float: right;margin-right:10px\"> </div><div class=\"clearBoth\"></div> </div>     </div>";
            }
            return html;
        }
        #endregion

        #region 添加对帖子的评论
        /// <summary>
        /// 添加对帖子的评论
        /// </summary>
        /// <param name="topic">话题对象</param>
        /// <returns></returns>
        public ActionResult ADDtopic(Topic topic)
        {
            topic.TopicTime = DateTime.Now;
            bool s = topicbll.AddEntity(topic);
            if (s)
            {
                return Content("<script>alert('添加成功');location.href='/Home/showTop/" + topic.TId + "'</script>");
            }
            else
            {
                return Content("<script>alert('失败');location.href='/Home/showTop'</script>");
            }
        }
        #endregion

        #region 添加帖子
        /// <summary>
        /// 添加帖子
        /// </summary>
        /// <returns></returns>
        public ActionResult ADDtie()
        {
            //接到课程ID
            int CourseID = Convert.ToInt32(Session["ClassId"]);
            Courses course = coursebll.Search(CourseID);
            ViewBag.course = course;
            return View();
        }
        #endregion

        #region 添加帖子方法
        /// <summary>
        /// 添加帖子方法
        /// </summary>
        /// <param name="topic">帖子对象</param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult ADDtie(Topic topic)
        {
            topic.TopicTime = DateTime.Now;
            bool s = topicbll.AddEntity(topic);
            if (s)
            {
                return Content("<script>alert('添加成功');location.href='/Home/topics'</script>");
            }
            else
            {
                return Content("<script>alert('失败');location.href='/Home/ADDtie'</script>");
            }
        }
        #endregion

        #region 极客班首页
        /// <summary>
        /// 极客班首页
        /// </summary>
        /// <returns></returns>
        public ActionResult GeekClass()
        {
            return View();
        }
        #endregion

        public ActionResult cstd()
        {
            return View();
        }
    
        public ActionResult ShiTing()
        {
              CourseBLL bll = new CourseBLL();
              List<Courses> list = bll.chaxun(c => c.CoursesName.Contains("net")).ToList();
            return View("Index",list); 
        }
    
      
    
  

        
        
       
    }
}
